package com.ccom.security.handler;

import com.ccom.core.common.enums.ResultCodeEnum;
import com.ccom.core.common.exception.e.InnerAuthException;
import com.ccom.core.common.exception.e.PreAuthorizeException;
import com.ccom.core.common.model.Result;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import javax.servlet.http.HttpServletRequest;

/**
 * 异常处理器
 */
@RestControllerAdvice
public class GlobalExceptionHandler {
    private static final Logger log = LoggerFactory.getLogger(GlobalExceptionHandler.class);

    /**
     * 权限异常
     */
    @ExceptionHandler(PreAuthorizeException.class)
    public Result<?> handlePreAuthorizeException(PreAuthorizeException e, HttpServletRequest request) {
        String requestURI = request.getRequestURI();
        log.error("请求地址'{}',权限校验失败'{}'", requestURI, e.getMessage());
        return Result.error(ResultCodeEnum.REQ_REJECT);
    }

    /**
     * 内部认证异常
     */
    @ExceptionHandler(InnerAuthException.class)
    public Result<?> handleInnerAuthException(InnerAuthException e) {
        return Result.error(e.getMessage());
    }

}
